package com.example.controller;

import com.example.dto.StudentDTO;
import com.example.pojo.PageBean;
import com.example.pojo.Result;
import com.example.pojo.Student;
import com.example.service.StuService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/stus")
@RequiredArgsConstructor
public class StuController {

    private final StuService stuService;

    @PostMapping
    public Result insert(@RequestBody Student stu) {
        log.info("新增记录，stu:{}", stu);
        stuService.saveStudent(stu);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable String id) {
        log.info("删除记录，id:{}", id);
        stuService.removeById(id);
        return Result.success();
    }

    @PutMapping
    public Result updateById(@RequestBody Student stu) {
        log.info("更新记录，stu:{}", stu);
        stuService.updateStudent(stu);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable String id) {
        log.info("查询记录，student:{}", id);
        Student student = stuService.getById(id);
        return Result.success(student);
    }

    @GetMapping
    public Result find(StudentDTO studentDTO) {
        log.info("分页查询，参数：{}", studentDTO);
        //获取查询数据
        PageBean<Student> pageBean = stuService.find(studentDTO);

        return Result.success(pageBean);
    }
}
